package com.example.administrator.student4.Adapter;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.student4.R;
import org.json.JSONArray;
import org.json.JSONException;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * Created by 周千遇 on 2016/11/19.
 */
public class SearchByClassAdapter extends BaseAdapter {
    Context context;
    JSONArray array;
    ViewHolder viewHolder;
    int deleteposition = 0,deleteid;
    Myhandler myhandler;
    public SearchByClassAdapter(Context context, JSONArray array) {
        this.context = context;
        this.array = array;
    }

    @Override
    public int getCount() {
        return array.length();
    }

    @Override
    public Object getItem(int position) {
        Object o = null;
        try {
            o = array.getJSONObject(position);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return o;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    public void remove(int position){
        array.remove(position);
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        viewHolder = new ViewHolder();
        myhandler = new Myhandler();
        if(convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.searchbyclasslv,null);
            viewHolder.subject = (TextView) convertView.findViewById(R.id.subject);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.number = (TextView) convertView.findViewById(R.id.number);
            viewHolder.grade = (TextView) convertView.findViewById(R.id.grade);
            viewHolder.term = (TextView) convertView.findViewById(R.id.term);
            viewHolder.delete = (Button) convertView.findViewById(R.id.delete);
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        try {
            viewHolder.number.setText(array.getJSONObject(position).getString("number"));
            viewHolder.name.setText(array.getJSONObject(position).getString("name"));
            viewHolder.subject.setText(array.getJSONObject(position).getString("subject"));
            viewHolder.grade.setText(array.getJSONObject(position).getString("grade"));
            viewHolder.term.setText(array.getJSONObject(position).getString("term"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        viewHolder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        try {
                            deleteposition = position;
                            deleteid = array.getJSONObject(position).getInt("id");
                            String path = "http://10.0.2.2:8080/andorid/grade?request=delete&id="+deleteid;
                            URL url = new URL(path);
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                            connection.connect();
                            if(connection.getResponseCode()==200) connection.getInputStream();
                            myhandler.sendEmptyMessage(0x111);
                        } catch (JSONException | IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });
        return convertView;
    }
    class ViewHolder{
        TextView number,name,subject,grade,term;
        Button delete;
    }
    class Myhandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what ==0x111){
                remove(deleteposition);
                Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();
                notifyDataSetChanged();
            }
        }
    }
}
